
OPENMVG_SFM_BIN = "D:/code/c++/openMVG/openmvg_build/Windows-AMD64-Release/Release"

# Indicate the openMVG camera sensor width directory
CAMERA_SENSOR_WIDTH_DIRECTORY = "D:/code/c++/openMVG/src/software/SfM" + "/../../openMVG/exif/sensor_width_database"

import os
import subprocess
import sys

if len(sys.argv) < 4:
    print ("Usage %s image_dir output_dir type" % sys.argv[0])
    sys.exit(1)

def remove_suff_file(sf):
  listfiles= os.listdir("./")
  for f in listfiles:
    if os.path.isfile(f) and f.endswith(sf):
        os.remove(f) 

input_dir = sys.argv[1]
output_dir = sys.argv[2]
matches_dir = os.path.join(output_dir, "matches")
reconstruction_dir = os.path.join(output_dir, "reconstruction_sequential")
camera_file_params = os.path.join(CAMERA_SENSOR_WIDTH_DIRECTORY, "sensor_width_camera_database.txt")

print ("Using input dir  : ", input_dir)
print ("      output_dir : ", output_dir)

# Create the ouput/matches folder if not present
if not os.path.exists(output_dir):
  os.mkdir(output_dir)
if not os.path.exists(matches_dir):
  os.mkdir(matches_dir)

if sys.argv[3]=="1":

  print ("1. Intrinsics analysis")
  pIntrisics = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_SfMInit_ImageListing"),  "-i", input_dir, "-o", matches_dir, "-d", camera_file_params,"-P"] )
  pIntrisics.wait()

  print ("2. Compute features")
  pFeatures = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeFeatures"),  "-i", matches_dir+"/sfm_data.json", "-o", matches_dir, "-m", "SIFT","-n","9"] )
  pFeatures.wait()

  print("end compute features\n");

if sys.argv[3]=="2":

  print ("3. Compute matching pairs")
  pPairs = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_PairGenerator"), "-i", matches_dir+"/sfm_data.json", "-o" , matches_dir + "/pairs.bin" ] )
  pPairs.wait()

  print ("4. Compute matches")
  pMatches = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeMatches"),  "-i", matches_dir+"/sfm_data.json", "-p", matches_dir+ "/pairs.bin","-o", matches_dir + "/matches.putative.bin" ] )
  pMatches.wait()

  print ("5. Filter matches" )
  pFiltering = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_GeometricFilter"), "-i", matches_dir+"/sfm_data.json", "-m", matches_dir+"/matches.putative.bin" , "-g" , "f" , "-o" , matches_dir+"/matches.f.bin" ] )
  pFiltering.wait()
  print("end match")

if sys.argv[3]=="3":

  if not os.path.exists(reconstruction_dir):
    os.mkdir(reconstruction_dir)

  print ("6. Do Sequential/Incremental reconstruction")
  pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_SfM"), "--sfm_engine", "GLOBAL", "--input_file", matches_dir+"/sfm_data.json", "--match_dir", matches_dir, "--output_dir", reconstruction_dir] )
  pRecons.wait()

  print ("7. Colorize Structure")
  pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeSfM_DataColor"),  "-i", reconstruction_dir+"/sfm_data.bin", "-o", os.path.join(reconstruction_dir,"colorized.ply")] )
  pRecons.wait()

  # print ("8. Structure from Known Poses (robust triangulation)")
  # pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeStructureFromKnownPoses"),  "-i", reconstruction_dir+"/sfm_data.bin", "-m", matches_dir, "-o", os.path.join(reconstruction_dir,"robust.ply")] )
  # pRecons.wait()
  print("end sfm")


if sys.argv[3]=="4":
  print ("9. openmvg2openmvs")
  pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_openMVG2openMVS"),  "-i", reconstruction_dir+"/sfm_data.bin",  "-o", "./out/scene.mvs"] )
  pRecons.wait()

  print ("10. densifycloud")
  pRecons = subprocess.Popen(  ["DensifyPointCloud", "-i","./out/scene.mvs","-o","./out/dense.ply", "--resolution-level", "6",  "--cuda-device", "2"])
  pRecons.wait()
  remove_suff_file(".dmap")
  
  print("end densify")





if sys.argv[3]=="5":
  print ("11. reconstruct")
  pRecons = subprocess.Popen( ["ReconstructMesh",  "./out/dense.mvs", "--max-threads","9","-o","./out/remesh.ply"] )
  pRecons.wait()
  ###target-face-num
  print("refine mesh\n") 
  pRecons = subprocess.Popen( ["RefineMesh",  "./out/remesh.mvs", "--max-threads","9","--resolution","3","-o","./out/refine.ply"] )
  pRecons.wait()
  

  print ("14. texture mesh")
  pRecons=subprocess.Popen(["TextureMesh.exe",  "./out/refine.mvs", "--resolution-level", "3",  "--max-threads", "9", "-o", "./out/refine_texture_mesh.ply"])
  pRecons.wait()

  
  print("end point2mesh")



### os.listdir()
### os.path.isfile()
### os.endswith()
